

//
//  SelectedView.m
//  01-SelectedView
//
//  Created by fanzhuxian on 2017/6/30.
//  Copyright © 2017年 FanZhuxian. All rights reserved.
//

#import "SelectedView.h"

#define Screen_height [[UIScreen mainScreen] bounds].size.height
#define Screen_width [[UIScreen mainScreen] bounds].size.width

@interface SelectedView ()

@property (nonatomic, strong) NSArray * buttonName;

//@property (nonatomic, strong) UILabel * underLine;

@property (nonatomic, strong) UILabel * downLine;

@property (nonatomic, strong) UIButton * selectButton;

@property (nonatomic ,strong) NSArray * controllers;

@property (nonatomic, strong) UIView * selectedView;

@property (nonatomic, strong) UIScrollView * selectedScrollV;

@end

@implementation SelectedView

- (instancetype)initWithFrame:(CGRect)frame buttonName:(NSArray *)buttonName controllers:(NSArray *)controllers parentController:(UIViewController *)parentC {
    
    if (self = [super initWithFrame:frame]) {
        self.buttonName = buttonName;
        self.controllers = controllers;
        
        // 创建selectedView
        self.selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, 48)];
        self.selectedView.tag = 50;
        [self addSubview:self.selectedView];
        
        // 创建selectedScrollV
        self.selectedScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 48, Screen_width, frame.size.height)];
        self.selectedScrollV.contentSize = CGSizeMake(Screen_width * self.controllers.count, 0);
        self.selectedScrollV.showsHorizontalScrollIndicator = NO;
        self.selectedScrollV.pagingEnabled = YES;
        self.selectedScrollV.bounces = YES;
        self.selectedScrollV.delegate = self;
        [self addSubview:self.selectedScrollV];
        
        // 创建ViewController并添加到selectedScrollV上
        for (int i = 0; i < self.controllers.count; i++) {
            
            UIViewController * VC = self.controllers[i];
            VC.view.frame = CGRectMake(i * Screen_width, 0, Screen_width, frame.size.height);
            [self.selectedScrollV addSubview:VC.view];
            [parentC addChildViewController:VC];
            // 子视图交换时使用
            [VC didMoveToParentViewController:parentC];
        }
        
        // 创建selectedButton和line
        for (int i = 0; i < self.controllers.count; i++) {
            
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        
            button.frame = CGRectMake(i * (Screen_width / self.controllers.count), 0, Screen_width / self.controllers.count, 48);
            button.tag = i;
            [button setTitle:self.buttonName[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
           
            if (i == 0) {
                
                button.selected = YES;
                self.selectButton = button;
                button.titleLabel.font = [UIFont systemFontOfSize:15];
            
            } else {
            
            button.selected = NO;
        
            }
            [self.selectedView addSubview:button];
        }
        
        // 添加下划线
        self.underLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 46, Screen_width / self.controllers.count, 1.5)];
        self.underLine.backgroundColor = [UIColor blueColor];
        self.underLine.tag = 70;
        [self.selectedView addSubview:self.underLine];
        
        self.downLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 47.5, Screen_width, 0.5)];
        self.downLine.backgroundColor = [UIColor grayColor];
        [self.selectedView addSubview:self.downLine];
    }
        return self;
}

// 点击方法
- (void)Click:(UIButton *)sender {

    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:15];
    self.selectButton.selected = NO;
    self.selectButton = sender;
    self.selectButton.selected = YES;
    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [UIView animateWithDuration:0.2 animations:^{
        
        CGPoint frame = self.underLine.center;
        frame.x = Screen_width / (self.controllers.count * 2) + (Screen_width / self.controllers.count) * (sender.tag);
        self.underLine.center = frame;
    }];
    
    [self.selectedScrollV setContentOffset:CGPointMake((sender.tag) * Screen_width, 0) animated:YES];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        CGPoint frame = self.underLine.center;
        frame.x = Screen_width / (self.controllers.count * 2) + (Screen_width / self.controllers.count) * (self.selectedScrollV.contentOffset.x / Screen_width);
        self.underLine.center = frame;
    }];
    
    UIButton * button = (UIButton *)[self.selectedView viewWithTag:self.selectedScrollV.contentOffset.x / Screen_width];
    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:15];
    self.selectButton.selected = NO;
    self.selectButton = button;
    self.selectButton.selected = YES;
    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:15];
}


@end
